package com.v9.flow.service;

import com.v9.flow.service.dto.LeaveTask;
import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class TaskInfoService {
    @Resource
    private TaskService taskService;
    @Resource
    protected HistoryService historyService;
    /**
     * 查询用户待办任务列表。
     *
     * @param assignee 用户
     * @return 任务列表
     */
    public List<LeaveTask> queryToDoTasks(String assignee) {
        List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
        List<LeaveTask> leaveTasks = new ArrayList<>();
        for (Task task : list) {
            LeaveTask leaveTask = new LeaveTask();
            leaveTask.setTaskId(task.getId());
            leaveTask.setName(task.getName());
            leaveTask.setProcessDefinitionId(task.getProcessDefinitionId());
            leaveTasks.add(leaveTask);
        }
        return leaveTasks;
    }

    /**
     * 查询已处理任务列表。
     *
     * @param assignee 用户
     * @return 已处理任务列表
     */
    public List<LeaveTask> queryDoneTasks(String assignee) {
        List<HistoricTaskInstance> taskList  = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(assignee)
                .finished()
                .list();

        List<LeaveTask> leaveTasks = new ArrayList<>();
        for (HistoricTaskInstance task : taskList) {
            LeaveTask leaveTask = new LeaveTask();
            leaveTask.setTaskId(task.getId());
            leaveTask.setName(task.getName());
            leaveTask.setProcessDefinitionId(task.getProcessDefinitionId());
            leaveTasks.add(leaveTask);
        }
        return leaveTasks;
    }


}
